import java.util.ArrayList;
import java.util.List;

public class DataAccess {
    // 影院厅信息
    public static final String[] HALLS = {"黄金厅", "白银厅", "翡翠厅", "钻石厅", "宝石厅"};
    public static final int[] HALL_SEATS = {50, 40, 30, 25, 20}; // 各厅座位数

    // 数据存储列表
    private static List<User> users;
    private static List<Movie> movies;
    private static List<Schedule> schedules;

    // 静态初始化，从文件加载数据
    static {
        users = FileDataStorage.loadUsers();
        movies = FileDataStorage.loadMovies();
        schedules = FileDataStorage.loadSchedules();
    }

    // 用户相关操作
    public static boolean addUser(User user) {
        // 检查用户名是否已存在
        if (findUserByUsername(user.getUsername()) != null) {
            return false;
        }
        boolean result = users.add(user);
        if (result) {
            FileDataStorage.saveUsers(users); // 保存到文件
        }
        return result;
    }

    public static User findUserByUsername(String username) {
        for (User user : users) {
            if (user.getUsername().equals(username)) {
                return user;
            }
        }
        return null;
    }

    public static List<User> getAllUsers() {
        return new ArrayList<>(users); // 返回副本，防止外部直接修改
    }

    public static boolean deleteUser(String username) {
        User user = findUserByUsername(username);
        if (user != null) {
            boolean result = users.remove(user);
            if (result) {
                FileDataStorage.saveUsers(users); // 保存到文件
            }
            return result;
        }
        return false;
    }

    public static void updateUser(User user) {
        // 查找并替换用户
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getUsername().equals(user.getUsername())) {
                users.set(i, user);
                FileDataStorage.saveUsers(users); // 保存到文件
                break;
            }
        }
    }

    // 电影相关操作
    public static boolean addMovie(Movie movie) {
        // 检查ID是否已存在
        if (findMovieById(movie.getId()) != null) {
            return false;
        }
        boolean result = movies.add(movie);
        if (result) {
            FileDataStorage.saveMovies(movies); // 保存到文件
        }
        return result;
    }

    public static Movie findMovieById(String id) {
        for (Movie movie : movies) {
            if (movie.getId().equals(id)) {
                return movie;
            }
        }
        return null;
    }

    public static List<Movie> getAllMovies() {
        return new ArrayList<>(movies); // 返回副本
    }

    public static boolean deleteMovie(String id) {
        Movie movie = findMovieById(id);
        if (movie != null) {
            // 检查是否有关联的场次
            if (getSchedulesByMovieId(id).size() > 0) {
                return false;
            }

            boolean result = movies.remove(movie);
            if (result) {
                FileDataStorage.saveMovies(movies); // 保存到文件
            }
            return result;
        }
        return false;
    }

    public static void updateMovie(Movie movie) {
        // 查找并替换电影
        for (int i = 0; i < movies.size(); i++) {
            if (movies.get(i).getId().equals(movie.getId())) {
                movies.set(i, movie);
                FileDataStorage.saveMovies(movies); // 保存到文件
                break;
            }
        }
    }

    // 场次相关操作
    public static boolean addSchedule(Schedule schedule) {
        // 检查场次ID是否已存在
        if (findScheduleById(schedule.getId()) != null) {
            return false;
        }
        boolean result = schedules.add(schedule);
        if (result) {
            FileDataStorage.saveSchedules(schedules); // 保存到文件
        }
        return result;
    }

    public static Schedule findScheduleById(String id) {
        for (Schedule schedule : schedules) {
            if (schedule.getId().equals(id)) {
                return schedule;
            }
        }
        return null;
    }

    public static List<Schedule> getSchedulesByMovieId(String movieId) {
        List<Schedule> result = new ArrayList<>();
        for (Schedule schedule : schedules) {
            if (schedule.getMovieId().equals(movieId)) {
                result.add(schedule);
            }
        }
        return result;
    }

    public static List<Schedule> getAllSchedules() {
        return new ArrayList<>(schedules); // 返回副本
    }

    public static boolean deleteSchedule(String id) {
        Schedule schedule = findScheduleById(id);
        if (schedule != null) {
            boolean result = schedules.remove(schedule);
            if (result) {
                FileDataStorage.saveSchedules(schedules); // 保存到文件
            }
            return result;
        }
        return false;
    }

    public static void updateSchedule(Schedule schedule) {
        // 查找并替换场次
        for (int i = 0; i < schedules.size(); i++) {
            if (schedules.get(i).getId().equals(schedule.getId())) {
                schedules.set(i, schedule);
                FileDataStorage.saveSchedules(schedules); // 保存到文件
                break;
            }
        }
    }
}
